
//打开窗口
function openwindow(str_url,width,height,target){
	if(str_url=='') return false;
	
	if(target==null || target=='') target = "_blank";
	//if(height==null || height=='') height = screen.height;
	//if(width==null || width=='') width = screen.width;
	var str_style = "";
	if (width==null || width=='' || height==null || height=='')
	{
	 	//str_style = "toolbar=0, menubar=0, scrollbars=1, resizable=1, fullscreen=yes";
	 	str_style = "toolbar=0, menubar=0, scrollbars=1, resizable=1, width=" + width + ", height=" + height + ", left=" + left + ", top=" + top;
	 	var newwindow = window.open(str_url,target,str_style);
		newwindow.moveTo(0,0);
		newwindow.resizeTo(screen.availWidth,screen.availHeight)
	}
	else
	{
		var left = (screen.width - width)/2;
		var top = (screen.height - height)/2;
	
		//var str_style = "toolbar=0, menubar=0, scrollbars=1, resizable=1, width=" + width + ", height=" + height + ", left=" + left + ", top=" + top;
	 	str_style = "toolbar=0, menubar=0, scrollbars=1, resizable=1, width=" + width + ", height=" + height + ", left=" + left + ", top=" + top;
	 	window.open(str_url,target,str_style);
	}
	//????
	//width = screen.width - 15;;
	//height = screen.height - 15;
	
	
}
//打开窗口
function openwindow1(str_url,width,height,target){
	if(str_url=='') return false;
	
	if(target==null || target=='') target = "_blank";
	//if(height==null || height=='') height = screen.height;
	//if(width==null || width=='') width = screen.width;
	var str_style = "";
	if (width==null || width=='' || height==null || height=='')
	{
	 	//str_style = "toolbar=0, menubar=0, scrollbars=1, resizable=1, fullscreen=yes";
	 	str_style = "toolbar=no, menubar=no, scrollbars=1, resizable=1, width=" + width + ", height=" + height + ", left=" + left + ", top=" + top;
	 	 
	 	var newwindow = window.open(str_url,target,str_style);
		newwindow.moveTo(0,0);
		newwindow.resizeTo(screen.availWidth,screen.availHeight)
	}
	else
	{   
		var left = (screen.width - width)/2;
		var top = (screen.height - height)/2;
	
		//var str_style = "toolbar=0, menubar=0, scrollbars=1, resizable=1, width=" + width + ", height=" + height + ", left=" + left + ", top=" + top;
	 	str_style = "toolbar=0, menubar=0, scrollbars=1, resizable=no, width=" + width + ", height=" + height + ", left=" + left + ", top=" + top;
	 	window.open(str_url,target,str_style);
	}
	//????
	//width = screen.width - 15;;
	//height = screen.height - 15;
	
	
}
function checkLength( obj , length , errMsg , formName )
{
    var stringLength = obj.value.replace(/[^\x00-\xff]/g,"**").length;
    if( stringLength > length )
    {   
        errAlert( obj , errMsg , formName );
        return false;
    }
    return true;
}
/*
field   the Name of the Element to check
 name    the display Name
 len     then length of num
*/
function validateNum(field,name,len)  //  1. 必须输入整数,并进行长度校验
{
	var valid ="-0123456789";
	var ok="yes";
	var temp;
	for (var i=0; i<field.value.length; i++)
	{
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1")
		ok = "no";
		else{
			if(temp=="-" && i>0) ok="no";
		}
	}
	if(ok=="no")
	{
		alert(name+"必须输入整数");
		field.focus();
		field.select();
		return false;
	}
	if(field.value.length>len)
	{
		alert(name+"长度不能大于"+len);
		field.focus();
		field.select();
		return false;
	}
	return true;
}
/*
 name  域的中文名
 */
function validateChar(field,name,len)  //  2. char 校验
{
	if(field.value.length>len)
	{
		alert(name+"长度不能大于"+len);
		field.focus();
		field.select();
		return false;
	}
	return true;
}
/*
 field
 name
 len  浮点数最大长度,不包括小数位
flen 小数位最大长度
*/
function validateNumDot(field,name,len,flen)  // 3. 浮点数校验
{
	var pattern =/^[0-9,-]{0,}[.]{0,1}[0-9]{0,}$/;
	var flag;
	flag = pattern.test(field.value);
	
	
	if(!flag)
	{
		alert(name+"格式不正确! 请输入最大长度为: " + len + ", 其中小数位最大长度为: " + flen +" 的浮点数");
		field.focus();
		field.select();
		return false;
	}
	//数字长度
	//alert("total :"+field.value.length);
	if(field.value.length -1 > len)
	{
		alert(name+"浮点数位总长度不能大于"+len);
		return false;
	}
/*	
	//整数长度
	//alert("len :"+len);
	
	if(field.value.length - 1 > len - flen)
	{
		alert(name+"整数位总长度不能大于"+len - flen);
		return false;
	}
	//小数位长度
	var leng =0;
	if(field.value.indexOf(".")=="-1")
	{
		leng = 0;
	}else
	{
		leng = field.value.length - 1 - field.value.indexOf(".");
	}
	if(leng > flen)
	{		
		alert(name+"小数位长度不能大于"+flen);
		return false;
	}
*/
//定义三个变量记录长度
   var leng =0;
   var temp=0;
   var mytemp=0;
//如果输入的是整数并且长度大了返回false   
if(field.value.length >len-flen && field.value.indexOf(".")=="-1")
{
  alert(name+",整数长度不能大于"+len-flen);
  return false;
}
if(field.value.indexOf(".")==0)
	{
  alert(name+",输入的首位为'.',请重输！");
  return false;
	}
//如果最后一位是".",返回为false
if(field.value.length >0 && field.value.indexOf(".")==field.value.length-1)
{
  alert(name+",输入的最后一位为'.',请重输！");
  return false;
}

//返回'.'的位置
if(field.value.indexOf(".")=="-1")
{
		leng = 0;
		temp=0;
}
else
{
	leng = field.value.length - 1 - field.value.indexOf(".");
	temp= field.value.indexOf(".");
	mytemp=field.value.indexOf(".")-1;
		 
}
if(temp > len - flen)
{
alert(name+",整数位总长度不能大于"+ mytemp);

return false;
}
if(leng > flen)
{		
	alert(name+",小数位长度不能大于"+flen);
	return false;
		 
}
	return true;
}



function clskg(va){
	  var th=/\s/g;
	  va=va.replace(th,'');
	 return va;
	}
function validateNotNull(field,name)  // 4. 非空校验
{   
	if(clskg(field.value)=="")
	
	{
		alert(name+"不能为空");
		field.focus();
		//field.select();
		return false;
	}
	return true;
}

/*
parameter model, 字符串或整数 : {c,i}_{y,n}_{1..9}{0..9}... //类型_可否为空_总长度
parameter model, 浮点数 : {f}_{y,n}_{1..9}_{0..9}...  //类型_可否为空_小数位长度_总长度
example: c_n_20 , c_y_10 , i_n_12 , f_y_2_15
在<input>标签中加入: check="i_n_5"
*/
function submitCheck(formName, frameName)  //  5.是否提交的校验
{
  if(frameName != null){
    formToCheck = eval("document." + frameName + "." + formName);
  }else if(formName != null){
    formToCheck = eval("document." + formName);
  }else{
    formToCheck = document.forms[0];
  }
  var type;
  var canNull;
  var maxLength = 0;
  var fLength = 0;
  for(i = 0; i < formToCheck.elements.length; i++){
    if(formToCheck.elements[i].check == null){
      continue;
    }
    type = formToCheck.elements[i].check.slice(0, 1);
    canNull = formToCheck.elements[i].check.slice(2, 3);
    if(type == "f"){
      fLength= formToCheck.elements[i].check.slice(4,5);
      maxLength = formToCheck.elements[i].check.slice(6,100);
    }else{
      maxLength = formToCheck.elements[i].check.slice(4,100);
    }
    if(formToCheck.elements[i].value == ""){
      if(canNull == "n"){
      	alert("输入有误! 不能为空")
      	formToCheck.elements[i].focus();
	formToCheck.elements[i].select();
      	return false;
      }else{
        continue;
      }
    }
    if(type == "c"){
      if(formToCheck.elements[i].value.length > maxLength){
      	alert("输入有误! 超出长度限制: "+maxLength)
      	formToCheck.elements[i].focus();
	formToCheck.elements[i].select();
      	return false;
      }
    }else if(type == "i"){
      result = validateNum(formToCheck.elements[i]," ",maxLength);
      if(!result){
      	formToCheck.elements[i].focus();
	formToCheck.elements[i].select();
        return false;
      }
    }else{
      result = validateNumDot(formToCheck.elements[i]," ",maxLength, fLength);
      if(!result){
      	formToCheck.elements[i].focus();
	formToCheck.elements[i].select();
        return false;
      }
    }
  }
  return true;
}